package com.appbee.gdata.picasa.client.place;

import com.appbee.core.client.place.INamePlace;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceTokenizer;
import com.google.gwt.place.shared.Prefix;

public class ThumbnailPlace extends Place implements INamePlace
{
	private String albumId;
	private String name;
	private int index = -1;
	
	public ThumbnailPlace()
	{}

	public ThumbnailPlace(String name, String albumid, int index)
	{
		this.name = name;
		this.albumId = albumid;
		this.index = index;
	}
	public String getAlbumId()
	{
		return albumId;
	}
	
	@Override
	public String getName()
	{
		return name;
	}

	@Override
	public String[] getPathElements()
	{
		return name.split("_");
	}

	@Override
	public String getRootElement()
	{
		return getPathElements()[0];
	}

	public int getIndex()
	{
		return index;
	}

	@Prefix("mtn")
	public static class Tokenizer implements PlaceTokenizer<ThumbnailPlace>
	{

		@Override
		public ThumbnailPlace getPlace(String token)
		{
			String[] split = token.split(":");
			ThumbnailPlace  p = new ThumbnailPlace();
			if(split.length>0) p.name = split[0];
			if(split.length>1) p.albumId = split[1];
			if(split.length>2) p.index = Integer.parseInt(split[2]);
			return p;
		}

		@Override
		public String getToken(ThumbnailPlace place)
		{
			if(place==null) return "";
			
			StringBuffer sb = new StringBuffer();
			sb.append(place.getName());
			sb.append(":");
			sb.append(place.getAlbumId());
			sb.append(":");
			sb.append(place.getIndex());
			return sb.toString();
		}
		
	}
	
}
